$(function () {
    var curriculumtable2_datagrid = $("#curriculumtable2_datagrid");
    var curriculumtable_datagrid = $("#curriculumtable_datagrid");
    var study_date = $("[name=study_date]");
    var curriculumtable_dialog = $("#curriculumtable_dialog");
    var curriculumtable2_form = $("#curriculumtable2_form");
    var curriculumtable_form = $("#curriculumtable_form");
    var form_btns = $("#form_btns");

    study_date.calendar({
        onSelect: function (date) {
            var calendarDate = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate()
                + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
            console.log(calendarDate);
            // $.post()
            // var cd = new Date(calendarDate);
            // console.log(cd);
            // $.post('/curriculumtable/query.do',{date:calendarDate},function (data) {
            //     curriculumtable2_datagrid.datagrid("load");
            // });
            curriculumtable2_datagrid.datagrid("load", {
                date: calendarDate
            })
        }
    });


    curriculumtable2_datagrid.datagrid({
        height: 600,
        toolbar: '#toolbar',
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        sortName: "sn",
        sortOrder: "asc",
        sortOrder: "desc",
        url: '/curriculumtable/list',
        columns: [[
            {
                field: 'classdate', title: '上课日期', width: 50
            },
            {
                field: 'classweek', title: '星期', width: 50
            },
            {
                field: 'grades', title: '班级', width: 50
            },
            {
                field: 'curriculums', title: '课程', width: 50
            },
            {
                field: 'headteacher', title: '班主任', width: 50
            },
            {
                field: 'teachstaff', title: '上课老师', width: 50
            },
            {
                field: 'classrooms', title: '教室', width: 50
            },
            {
                field: 'explanatorycommen', title: '备注', width: 50
            },
            {
                field: 'state', title: '状态', width: 20, formatter: function (value) {
                return value==0 ? "<font color='green'>可用</font>" : "<font color='red'>停用</font>";
            }
            }
        ]]
    })
    curriculumtable_dialog.dialog({
        width: 390,
        height: 350,
        closed: true,
        closable: true,
        buttons: form_btns,
        onClose: function () {
            curriculumtable2_form.form("clear");
        },
    })
    var methodObj = {
        //添加
        add: function () {
            curriculumtable_dialog.dialog("setTitle", "新增课程系列");
            curriculumtable_dialog.dialog('open');
        },
        //编辑
        edit: function () {
            //获取选中的行
            var row = curriculumtable2_datagrid.datagrid("getSelected");
            console.log(row);
            if (row == null) {
                $.messager.alert("提示", "请选择需要修改的行");
                return;
            }
                //表单加载数据
                curriculumtable_form.form("load", row);
                //设置弹窗标题
                $("#curriculumtable_dialog").dialog("setTitle", "修改课程系列");
                $("#curriculumtable_dialog").dialog({
                    width : 400,
                    height : 500
                })
                $("#curriculumtable_dialog").dialog("open");

        },
        //保存操作
        saveOrUpdate:function () {
            var id = $("#curriculumtableId").val();
            var url =  "/curriculumtable/save";
            if(id){
                url =  "/curriculumtable/update";
            }
            $("#curriculumtable_form").form('submit', {
                url: url,
                success: function (data) {
                    if($.parseJSON(data).success){
                        $.messager.alert('温馨提示',"操作成功");
                        //关闭框
                        curriculumtable_dialog.dialog("close");
                        //刷新列表数据
                        curriculumtable2_datagrid.datagrid("load");
                    }else{
                        $.messager.alert('温馨提示',$.parseJSON(data).msg);
                    }
                }
            })
        }
        ,
        // //保存更新
        // saveOrUpdate: function () {
        //     curriculumtable_form.form('submit', {
        //         url: '/curriculumtable/saveOrUpdate.do',
        //         success: function (data) {
        //             var data = $.parseJSON(data);
        //             // var data = eval('(' + data + ')');
        //             if (data.success) {
        //                 $.messager.alert("提示", data.msg, "info", function () {
        //                     curriculumtable_dialog.dialog(closed);   //关闭弹窗
        //                     curriculumtable_datagrid.datagrid("reload");  //刷新当前列表
        //
        //                 })
        //             } else {
        //                 $.messager.alert("提示", data.msg, "error");  //保存或更新失败，弹窗提示
        //             }
        //         }
        //     })
        // },
        //删除
        remove: function () {
            var row = curriculumtable2_datagrid.datagrid("getSelected");
            if (!row) {
                //如果没有选中 提示
                $.messager.alert("请选中要删除的课程");
                return;
            }
            $.messager.confirm('确认', "确定执行该操作吗", function (r) {
                if (r) {
                    $.get("/curriculumtable/delete", {id: row.id}, function (data) {
                        if (data.success) {

                            $.messager.alert("温馨提示","保存成功");
                            //关闭窗口
                            $("#curriculumtable_dialog").dialog("close");

                            $("#curriculumtable2_datagrid").datagrid("load")
                        } else {
                            $.messager.alert("温馨提示", data.msg)
                        }
                    })
                }
            });

        },
        //刷新
        reload: function () {
            curriculumtable2_datagrid.datagrid("load");
        },

        query: function () {
            var keyword = $("[name=keywords]").val();
            var curriculumtableId = $("#curriculumtableId").val();

            curriculumtable2_datagrid.datagrid('load', {
                keywords: keyword,
                curriculumtableId: curriculumtableId
            });
        },
        //高级查询
        /* query: function () {
         var keyword = $("[name=keywords]").val();
         var curriculumseriesId = $("#curriculumseriesId").val();

         curriculum_datagrid.datagrid('load', {
         keywords: keyword,
         curriculumseriesId:curriculumseriesId
         });
         },*/

    }

    $("a[data-cmd]").click(function () {
        //获取按键的data-cmd属性值
        var methodName = $(this).data('cmd');
        methodObj[methodName]();
    })
})